package com.southwind.mmall002.controller;


import com.southwind.mmall002.entity.Cart;
import com.southwind.mmall002.entity.User;
import com.southwind.mmall002.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 周恒
 * @since 2021-10-13
 */
@Controller
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartService cartService;
    @GetMapping("/add/{productId}/{price}/{quantity}")
    public ModelAndView add(
            @PathVariable("productId") Integer productId,
            @PathVariable("price")Float price,
            @PathVariable("quantity")Integer quantity,
            HttpSession session
    ){
        Cart cart = new Cart();
        cart.setProductId(productId);
        cart.setQuantity(quantity);
        cart.setCost(price*quantity);
        User user = (User) session.getAttribute("user");
        cart.setUserId(user.getId());
        ModelAndView modelAndView = new ModelAndView();
        if (cartService.save(cart)){
            modelAndView.setViewName("settlement1");
        }

        return modelAndView;

    }

}

